package com.ruoyi.xp.order.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.xp.order.domain.OrderItemGoods;
import com.ruoyi.xp.order.domain.OrderItemHeadPackage;
import com.ruoyi.xp.order.service.IOrderIncomeService;
import com.ruoyi.xp.order.service.IOrderItemGoodsService;
import com.ruoyi.xp.order.service.IOrderItemHeadPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xp.order.mapper.OrderItemMapper;
import com.ruoyi.xp.order.domain.OrderItem;
import com.ruoyi.xp.order.service.IOrderItemService;
import com.ruoyi.common.core.text.Convert;

/**
 * 虾皮订单商品明细信息Service业务层处理
 * 
 * @author chdx
 * @date 2023-01-22
 */
@Service
public class OrderItemServiceImpl implements IOrderItemService 
{
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private IOrderItemGoodsService orderItemGoodsService;

    @Autowired
    private IOrderItemHeadPackageService orderItemHeadPackageService;

    @Autowired
    private IOrderIncomeService orderIncomeService;

    /**
     * 查询虾皮订单商品明细信息
     * 
     * @param orderItemId 虾皮订单商品明细信息主键
     * @return 虾皮订单商品明细信息
     */
    @Override
    public OrderItem selectOrderItemByOrderItemId(Long orderItemId)
    {
        return orderItemMapper.selectOrderItemByOrderItemId(orderItemId);
    }

    /**
     * 查询虾皮订单商品明细信息列表
     * 
     * @param orderItem 虾皮订单商品明细信息
     * @return 虾皮订单商品明细信息
     */
    @Override
    public List<OrderItem> selectOrderItemList(OrderItem orderItem)
    {
        return orderItemMapper.selectOrderItemList(orderItem);
    }

    /**
     * 新增虾皮订单商品明细信息
     * 
     * @param orderItem 虾皮订单商品明细信息
     * @return 结果
     */
    @Override
    public int insertOrderItem(OrderItem orderItem)
    {
        return orderItemMapper.insertOrderItem(orderItem);
    }

    /**
     * 修改虾皮订单商品明细信息
     * 
     * @param orderItem 虾皮订单商品明细信息
     * @return 结果
     */
    @Override
    public int updateOrderItem(OrderItem orderItem)
    {
        return orderItemMapper.updateOrderItem(orderItem);
    }

    /**
     * 批量删除虾皮订单商品明细信息
     * 
     * @param orderItemIds 需要删除的虾皮订单商品明细信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderItemByOrderItemIds(String orderItemIds)
    {
        return orderItemMapper.deleteOrderItemByOrderItemIds(Convert.toStrArray(orderItemIds));
    }

    /**
     * 删除虾皮订单商品明细信息信息
     * 
     * @param orderItemId 虾皮订单商品明细信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderItemByOrderItemId(Long orderItemId)
    {
        return orderItemMapper.deleteOrderItemByOrderItemId(orderItemId);
    }

    /**
     * 查询没有出货信息的订单商品明细列表
     *
     * @return
     */
    @Override
    public List<OrderItem> selectOrderItemHaveGoodsOrHeadPackage() {
        return orderItemMapper.selectOrderItemHaveGoodsOrHeadPackage();
    }

    /**
     * 更新订单采购价
     *
     * @param orderSn
     */
    @Override
    public void updatePurchasePrice(String orderSn){

        OrderItem orderItemQuery = new OrderItem();
        orderItemQuery.setOrderSn(orderSn);
        List<OrderItem> orderItemList = orderItemMapper.selectOrderItemList(orderItemQuery);
        for (OrderItem orderItem : orderItemList) {
            OrderItemGoods orderItemGoodsQuery = new OrderItemGoods();
            orderItemGoodsQuery.setOrderSn(orderItem.getOrderSn());
            orderItemGoodsQuery.setModelId(orderItem.getModelId());
            List<OrderItemGoods> orderItemGoodsList = orderItemGoodsService.selectOrderItemGoodsList(orderItemGoodsQuery);
            OrderItemHeadPackage orderItemHeadPackageQuery = new OrderItemHeadPackage();
            orderItemHeadPackageQuery.setOrderSn(orderItem.getOrderSn());
            orderItemHeadPackageQuery.setModelId(orderItem.getModelId());
            List<OrderItemHeadPackage> orderItemHeadPackageList = orderItemHeadPackageService.selectOrderItemHeadPackageList(orderItemHeadPackageQuery);

            BigDecimal purchasePrice = new BigDecimal(0);
            BigDecimal purchaseTotalPrice = new BigDecimal(0);
            for (OrderItemGoods orderItemGoods : orderItemGoodsList) {
                purchasePrice = purchasePrice.add(orderItemGoods.getPurchasePrice() == null ? new BigDecimal(0) : orderItemGoods.getPurchasePrice());
                purchaseTotalPrice = purchaseTotalPrice.add(orderItemGoods.getPurchaseTotalPrice() == null ? new BigDecimal(0) : orderItemGoods.getPurchaseTotalPrice());
            }
            for (OrderItemHeadPackage orderItemHeadPackage : orderItemHeadPackageList) {
                purchasePrice = purchasePrice.add(orderItemHeadPackage.getPurchasePrice() == null ? new BigDecimal(0) : orderItemHeadPackage.getPurchasePrice());
                purchaseTotalPrice = purchaseTotalPrice.add(orderItemHeadPackage.getPurchaseTotalPrice() == null ? new BigDecimal(0) : orderItemHeadPackage.getPurchaseTotalPrice());
            }
            orderItem.setPurchasePrice(purchasePrice);
            orderItem.setPurchaseTotalPrice(purchaseTotalPrice);
            orderItemMapper.updateOrderItem(orderItem);

            orderIncomeService.updatePurchasePriceAndProfit(orderItem.getOrderSn());
        }

    }

}
